home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Multimedia / Movie3.0 / Source / xanim / xanim_txt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-02  |  5.5 KB  |  203 lines

  1.  
  2. /*
  3.  * xanim_txt.c
  4.  *
  5.  * Copyright (C) 1990,1991,1992,1993,1994 by Mark Podlipec. 
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified and redistributed without
  9.  * fee for non-commerical purposes provided that this copyright notice is
  10.  * preserved intact on all copies and modified copies.
  11.  * 
  12.  * There is no warranty or other guarantee of fitness of this software.
  13.  * It is provided solely "as is". The author(s) disclaim(s) all
  14.  * responsibility and liability with respect to this software's usage
  15.  * or its effect upon hardware or computer systems.
  16.  *
  17.  */
  18.  
  19. #include "xanim_gif.h"
  20.  
  21.  
  22. LONG Is_TXT_File();
  23. ULONG TXT_Read_File();
  24. void GIF_Free_Frame_List();
  25. GIF_FRAME *GIF_Read_File();
  26.  
  27. ULONG UTIL_Get_MSB_Long();
  28.  
  29. typedef struct
  30. {
  31.   ULONG frame_cnt;
  32.   GIF_FRAME *gframes;
  33. } TXT_FRAME_LST;
  34.  
  35. static char gif_file_name[256];
  36. static ULONG txt_max_imagex,txt_max_imagey,txt_max_imagec,txt_max_imaged;
  37.  
  38. /*
  39.  * This file will open the filename passed to it and determine if
  40.  * it is a txt91 file. If it is it returns TRUE, else FALSE. It
  41.  * closes the file before returning.
  42.  */
  43. LONG Is_TXT_File(filename)
  44. char *filename;
  45. {
  46.   FILE *fp;
  47.   ULONG firstword;
  48.  
  49.   if ( (fp=fopen(filename,XA_OPEN_MODE)) == 0) return(XA_NOFILE);
  50.   firstword = UTIL_Get_MSB_Long(fp);
  51.   fclose(fp);
  52.   /*                   t x t 9     so we ignore the 1 */
  53.   if (firstword == 0x74787439) return(TRUE);
  54.   return(FALSE);
  55. }
  56.  
  57. /*
  58.  * This file parse the txt animation file and converts it into actions. 
  59.  *
  60.  */
  61. ULONG TXT_Read_File(fname,anim_hdr)
  62. XA_ANIM_HDR *anim_hdr;
  63. char *fname;
  64. {
  65.   FILE *fp;
  66.   LONG ret,i,f_cnt;
  67.   LONG num_of_files;
  68.   LONG total_frame_cnt,txt_frame_cnt;
  69.   LONG *txt_frames,txtframe_num;
  70.   TXT_FRAME_LST *txt_frame_lst;
  71.  
  72.   txt_max_imagex = 0;
  73.   txt_max_imagey = 0;
  74.   txt_max_imagec = 0;
  75.   txt_max_imaged = 0;
  76.  
  77.   if ( (fp=fopen(fname,XA_OPEN_MODE))==0)
  78.   { 
  79.     fprintf(stderr,"Can't open %s for reading.\n",fname); 
  80.     return(FALSE);
  81.   }
  82.  
  83.   /* read and throw away txt91 header */
  84.   fscanf(fp,"%*s");
  85.  
  86.   /* Read the number of files */
  87.   fscanf(fp,"%ld",&num_of_files);
  88.   if (num_of_files <= 0)
  89.   {
  90.     fprintf(stderr,"num_of_file is invalid %ld\n",num_of_files);
  91.     fclose(fp);
  92.     return(FALSE);
  93.   }
  94.  
  95.   txt_frame_lst = (TXT_FRAME_LST *) 
  96.         malloc( sizeof(TXT_FRAME_LST) * num_of_files);
  97.   if (txt_frame_lst == 0) TheEnd1("TXT_Read_File: malloc err");
  98.  
  99.   /* Read in the GIF files, use only the 1st one's colormap
  100.    */
  101.   for(i=0; i<num_of_files; i++)
  102.   {
  103.     fscanf(fp,"%s",gif_file_name);
  104.     fprintf(stderr,"Reading %s\n",gif_file_name);
  105.     txt_frame_lst[i].gframes 
  106.     = GIF_Read_File(gif_file_name,anim_hdr,&ret);
  107.     txt_frame_lst[i].frame_cnt = ret;
  108.     if (anim_hdr->imagex > txt_max_imagex) txt_max_imagex = anim_hdr->imagex;
  109.     if (anim_hdr->imagey > txt_max_imagey) txt_max_imagey = anim_hdr->imagey;
  110.     if (anim_hdr->imagec > txt_max_imagec) txt_max_imagec = anim_hdr->imagec;
  111.     if (anim_hdr->imaged > txt_max_imaged) txt_max_imaged = anim_hdr->imaged;
  112.   }
  113.  
  114.   /* Check for Frame list at end of images.
  115.    */
  116.   ret=fscanf(fp,"%ld",&txtframe_num);
  117.   if ( (ret == 1) && (txtframe_num >= 0))
  118.   {
  119.     LONG tmp_txtframe; 
  120.  
  121.     /* read in txt frame list, keep track of actual frames since each
  122.      * txt_frame can have several frames(cmaps and images);
  123.      */
  124.     txt_frames = (LONG *) malloc(txtframe_num * sizeof(LONG) );
  125.     if (txt_frames == 0) TheEnd1("TXT_Read_File: frames malloc err");
  126.     total_frame_cnt = 0;
  127.     txt_frame_cnt   = 0;
  128.  
  129.     for(i=0; i < txtframe_num; i++)
  130.     {
  131.       ret = fscanf(fp,"%ld",&tmp_txtframe);
  132.       if ( (ret==1) && (tmp_txtframe >= 0) && (tmp_txtframe < num_of_files) )
  133.       {
  134.         txt_frames[txt_frame_cnt] = tmp_txtframe;
  135.         total_frame_cnt += txt_frame_lst[ tmp_txtframe ].frame_cnt;
  136.         txt_frame_cnt++;
  137.       }
  138.       else
  139.         fprintf(stderr,"TXT_READ: bad frame number (%ld) in frame list\n",
  140.                               tmp_txtframe);
  141.     }
  142.   } /* end of frame included at end */
  143.   else
  144.   {
  145.     txt_frames = (LONG *) malloc(num_of_files * sizeof(LONG) );
  146.     if (txt_frames == 0) TheEnd1("TXT_Read_File: no frames malloc err");
  147.  
  148.     txt_frame_cnt = num_of_files;
  149.     total_frame_cnt = 0;
  150.     for(i=0; i<txt_frame_cnt; i++)
  151.     {
  152.       txt_frames[i] = i;
  153.       total_frame_cnt += txt_frame_lst[ i ].frame_cnt;
  154.     }
  155.   }
  156.  
  157.   /* Allocate a frame_lst of that size.
  158.    */
  159.   anim_hdr->frame_lst 
  160.     = (XA_FRAME *)malloc(sizeof(XA_FRAME) * (total_frame_cnt + 1));
  161.   if (anim_hdr->frame_lst == NULL) TheEnd1("TXT_ANIM: frame malloc err");
  162.  
  163.   f_cnt = 0;
  164.   /* loop through valid frame list, adding gif_frames */
  165.   for(i = 0; i < txt_frame_cnt; i++)
  166.   {
  167.     ULONG k,frame_cnt;
  168.     GIF_FRAME *gtmp;
  169.  
  170.     gtmp      = txt_frame_lst[ txt_frames[i] ].gframes;
  171.     frame_cnt = txt_frame_lst[ txt_frames[i] ].frame_cnt;
  172.     k = 0;
  173.     while(gtmp != 0)
  174.     {
  175.       if (k >= frame_cnt)
  176.       {
  177.         fprintf(stderr,"TXT_Read_Anim: frame inconsistency %ld %ld\n",
  178.         k,frame_cnt);
  179.         break;
  180.       }
  181.       anim_hdr->frame_lst[f_cnt].time = gtmp->time;
  182.       anim_hdr->frame_lst[f_cnt].act  = gtmp->act;
  183.       gtmp = gtmp->next;
  184.       k++; f_cnt++;
  185.     } /* end of while */
  186.   } /* end of for */
  187.   anim_hdr->imagex = txt_max_imagex;
  188.   anim_hdr->imagey = txt_max_imagey;
  189.   anim_hdr->imagec = txt_max_imagec;
  190.   anim_hdr->imaged = txt_max_imaged;
  191.   anim_hdr->frame_lst[f_cnt].time = 0;
  192.   anim_hdr->frame_lst[f_cnt].act  = 0;
  193.   anim_hdr->loop_frame = 0;
  194.   anim_hdr->last_frame = f_cnt - 1;
  195.   FREE(txt_frames,0x4000); txt_frames=0;
  196.   for(i=0; i < num_of_files; i++)
  197.     GIF_Free_Frame_List(txt_frame_lst[i].gframes);
  198.   FREE(txt_frame_lst,0x4001); txt_frame_lst=0;
  199.   fclose(fp);
  200.   return(TRUE);
  201. }
  202.  
  203.